package com.xinyu126.net_trace.client

import android.content.Context
import android.location.Location
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.magical.location.client.ILocationClient
import com.magical.location.client.LocationListener
import com.magical.location.internal.BaseLocationRequest
import com.magical.location.model.LocationServiceState
import androidx.core.app.NotificationCompat

class BaiduLocationClient(context: Context) : BaseLocationRequest(context), ILocationClient {
    companion object Instance {
        private var inited = false
        private lateinit var _instance: BaiduLocationClient
        fun getClient(context: Context): BaiduLocationClient {
            if (!inited) {
                inited = true
                _instance = BaiduLocationClient(context)
            }
            return _instance;
        }
    }
    private val baiduClient = LocationClient(context)
    private var is_running = false
    init {

        baiduClient.registerLocationListener(object : BDAbstractLocationListener() {
            override fun onReceiveLocation(location: BDLocation?) {
                if (location != null) {
                    val lc = Location("baidu")
                    lc.latitude = location.latitude
                    lc.longitude = location.longitude
                    lc.altitude = location.altitude
                    lc.speed = location.speed
                    lc.time = System.currentTimeMillis()
                    lc.provider = "baidu"
                    lc.bearing = location.direction
                    lc.accuracy = location.radius
                    notifyLocationChanged(lc)
                }
            }
        })

        val locationCLientOption = LocationClientOption()
        locationCLientOption.locationMode = LocationClientOption.LocationMode.Hight_Accuracy
        locationCLientOption.setCoorType("bd09ll")
        locationCLientOption.setScanSpan(5000)
        locationCLientOption.isOpenGnss = true
        locationCLientOption.isLocationNotify = true
        locationCLientOption.setIgnoreKillProcess(true)
        locationCLientOption.setIsNeedAddress(true)
        locationCLientOption.setLocationPurpose(LocationClientOption.BDLocationPurpose.Sport)
        baiduClient.locOption = locationCLientOption

        // 设置后台任务前台化
        val notification = NotificationCompat.Builder(context, "baiduLocationService")
            .setContentTitle("位置服务")
            .setContentText("正在获取位置信息")
            .setSmallIcon(android.R.drawable.ic_menu_mylocation)
            .build()

        baiduClient.enableLocInForeground(666, notification)
    }

    override fun addListener(listener: LocationListener) {
        registerListener(listener)
    }

    override fun start() {
        is_running = true
        baiduClient.start()
        for(listener in listeners) {
            listener.onLocationServiceStateChanged(LocationServiceState.Connected)
        }
    }

    override fun stop() {
        is_running = false
        baiduClient.stop()
        for(listener in listeners) {
            listener.onLocationServiceStateChanged(LocationServiceState.Disconnected)
        }
    }


    override fun isRunning(): Boolean{
        return is_running
    }
}